﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Repository;
using PersonalHealth.Data.Model;
using Core.UnitOfWork;
using PersonalHealth.Data.Data;

namespace PersonalHealth.Data.Repository
{
    public class UserRepository : Repository<User>, IUserRepository
    {
        public UserRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        {

        }



        #region IUserRepository 成员

        public int GetMaxServiceNo()
        {
            HealthContext context = UnitOfWork.Context as HealthContext;
            string s = this.DbSet.Max(c => c.ServiceNo);
            if (string.IsNullOrEmpty(s))
                return 0;

            int i;
            if (int.TryParse(s, out i))
            {
                return i;
            }

            return 0;

        }

        public User Login(string serviceNo, string password)
        {
            return DbSet.Include("Sex").Include("Department").SingleOrDefault(c => c.ServiceNo == serviceNo && c.Password == password);
        }


        public User LoginWithIcCard(string icCardNo)
        {
            return DbSet.Include("Sex").Include("Department").SingleOrDefault(c => c.IcCard == icCardNo);
        }

        public User LoginWithIdCard(string idCardNo)
        {
            return DbSet.Include("Sex").Include("Department").SingleOrDefault(c => c.IdCard == idCardNo);
        }

        #endregion
    }
}
